//
//  RouteRequestResultModel.m
//  RouteSearcher
//
//  Created by wenqingzhou on 2/16/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RouteRequestResultModel.h"
 

@implementation RouteRequestResultModel

@synthesize arrayOfRoutes,numberOfRoutes,start,dest,url,tmpRoute,parserState,searchTime;

+(RouteRequestResultModel *)initWithRequest:(NSURL *)myUrl andStart:(NSString *)startStr andDest:(NSString *)destStr andSearchTime:(NSString *)time
{
	RouteRequestResultModel *myRRRM = [[RouteRequestResultModel alloc] init];
	myRRRM.url = myUrl;
	myRRRM.start=startStr;
	myRRRM.dest=destStr;
	myRRRM.searchTime=time;
	return [myRRRM autorelease];
	
}

-(BOOL)getResultOfRequest
{
	NSMutableURLRequest *request=[[NSMutableURLRequest alloc] initWithURL:self.url];
	[request setHTTPMethod:@"GET"];
	NSHTTPURLResponse *response;
	NSData *data= [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
	NSXMLParser *myXMLParser= [[[NSXMLParser alloc] initWithData:data] autorelease];
	myXMLParser.delegate =self;
	[request release];
	return [myXMLParser parse];
}

-(void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
 namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qualifiedName 
   attributes:(NSDictionary *)attributeDict
{
	
	if([elementName isEqualToString:@"MTRXML"])
	{
		self.parserState = 0;
		self.arrayOfRoutes = [[NSMutableArray alloc] init];
	}
	else if([elementName isEqualToString:@"ROUTE"])
	{
		self.parserState = 1;
		self.tmpRoute = [[RouteModel alloc] init];
		self.tmpRoute.arrayOfPath = [[NSMutableArray alloc] init];
	}
	else if([elementName isEqualToString:@"LENGTH"])
	{
		if (self.parserState == 1) {
			self.tmpRoute.duration = [[attributeDict objectForKey:@"time"] integerValue];
			self.tmpRoute.dist = [NSString stringWithFormat:@"%1.2f",[[attributeDict objectForKey:@"dist"] floatValue]/1000];
		}
		else if(self.parserState == 2)
		{
			self.tmpRoute.tmpPath.duration = [[attributeDict objectForKey:@"time"] integerValue];
			self.tmpRoute.tmpPath.dist = [NSString stringWithFormat:@"%1.2f",[[attributeDict objectForKey:@"dist"] floatValue]/1000]; 
		}

	}
	else if([elementName isEqualToString:@"WALK"] || [elementName isEqualToString:@"LINE"])
	{
		self.parserState = 2;
		self.tmpRoute.tmpPath = [[PathModel alloc] init];
		self.tmpRoute.tmpPath.arrayOfPoints = [[NSMutableArray alloc] init];
		self.tmpRoute.tmpPath.type = [attributeDict objectForKey:@"type"];
		if([self.tmpRoute.tmpPath.type isEqualToString:@"6"])
		{
			self.tmpRoute.tmpPath.code = @"Metro";
		}
		else if([self.tmpRoute.tmpPath.type isEqualToString:@"12"]||[self.tmpRoute.tmpPath.type isEqualToString:@"13"])
		{
			NSString *character=[[[attributeDict objectForKey:@"code"] substringFromIndex:4] substringToIndex:2];
			self.tmpRoute.tmpPath.code = [NSString stringWithFormat:@"%@",character];
		}
		else {
			
			NSString *number=[[[attributeDict objectForKey:@"code"] substringFromIndex:1] substringToIndex:4];
			NSString *character=[[[attributeDict objectForKey:@"code"] substringFromIndex:4] substringToIndex:2];
			self.tmpRoute.tmpPath.code = [NSString stringWithFormat:@"%d%@",[number integerValue],character];
		}
}
	else if([elementName isEqualToString:@"POINT"]&& self.parserState ==2)
	{
		self.parserState = 3;
		self.tmpRoute.tmpPath.tmpPoint = [[PointModel alloc] init];
		self.tmpRoute.tmpPath.tmpPoint.xCode=[attributeDict objectForKey:@"x"];
		self.tmpRoute.tmpPath.tmpPoint.yCode=[attributeDict objectForKey:@"y"];
		self.tmpRoute.tmpPath.tmpPoint.uid=[attributeDict objectForKey:@"uid"];
	}
	else if([elementName isEqualToString:@"ARRIVAL"])
	{
		if(self.parserState==3)
		{
			self.tmpRoute.tmpPath.tmpPoint.arriveTime=[Formatter timeToFormattedTime:[attributeDict objectForKey:@"time"]];
		}else if (self.parserState == 4) {
			self.tmpRoute.tmpPath.tmpStop.arriveTime=[Formatter timeToFormattedTime:[attributeDict objectForKey:@"time"]];
		}

	}
	else if([elementName isEqualToString:@"DEPARTURE"])
	{
		if(self.parserState==3)
		{
			self.tmpRoute.tmpPath.tmpPoint.departureTime=[Formatter timeToFormattedTime:[attributeDict objectForKey:@"time"]];
		}else if (self.parserState == 4) {
			self.tmpRoute.tmpPath.tmpStop.departureTime=[Formatter timeToFormattedTime:[attributeDict objectForKey:@"time"]];
		}
	}
	else if([elementName isEqualToString:@"STOP"]&& self.parserState ==2)
	{
		self.parserState = 4;
		self.tmpRoute.tmpPath.tmpStop = [[StopModel alloc] init];
		self.tmpRoute.tmpPath.tmpStop.xCode=[attributeDict objectForKey:@"x"];
		self.tmpRoute.tmpPath.tmpStop.yCode=[attributeDict objectForKey:@"y"];
		self.tmpRoute.tmpPath.tmpStop.id=[attributeDict objectForKey:@"id"];
	}
	else if([elementName isEqualToString:@"NAME"]&&self.parserState == 4)
	{
		if ([[attributeDict objectForKey:@"lang"] isEqualToString:@"2"])
		{
			self.tmpRoute.tmpPath.tmpStop.name=[attributeDict objectForKey:@"val"];
		}
	}
}

-(void)parser:(NSXMLParser *)parser 
didEndElement:(NSString *)elementName 
 namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qualifiedName 
{
	if([elementName isEqualToString:@"ROUTE"])
	{
		NSLog(@"addRoute:%@",self.tmpRoute);
		self.parserState = 0;
		[self.arrayOfRoutes addObject:self.tmpRoute];
		[self.tmpRoute release];
	}
	else if([elementName isEqualToString:@"WALK"] || [elementName isEqualToString:@"LINE"])
	{
		NSLog(@"addLine:%@",self.tmpRoute.tmpPath);
		self.parserState = 1;
		[self.tmpRoute.arrayOfPath addObject:self.tmpRoute.tmpPath];
		[self.tmpRoute.tmpPath release];
	}
	else if([elementName isEqualToString:@"POINT"]&& self.parserState ==3)
	{
		NSLog(@"addPoint:%@",self.tmpRoute.tmpPath.tmpPoint);
		self.parserState = 2;
		[self.tmpRoute.tmpPath.arrayOfPoints addObject:self.tmpRoute.tmpPath.tmpPoint];
		[self.tmpRoute.tmpPath.tmpPoint release];
	}
	else if([elementName isEqualToString:@"STOP"]&& self.parserState ==4)
	{
		NSLog(@"addStop:%@",self.tmpRoute.tmpPath.tmpStop);
		self.parserState = 2;
		[self.tmpRoute.tmpPath.arrayOfPoints addObject:self.tmpRoute.tmpPath.tmpStop];
		[self.tmpRoute.tmpPath.tmpStop release];
	}
}

- (NSString *)getTitleOfRoute
{
	return [NSString stringWithFormat:@"%@,%@-->%@",self.searchTime,self.start,self.dest];
}

-(void)dealloc
{
	[self.url release];
	[self.arrayOfRoutes release];
	[self.tmpRoute release];
	[self.start release];
	[self.dest release];
	[super dealloc];	
}

@end
